﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Chinesys.Entities.Constants;
using Chinesys.Domain;
using Chinesys.Entities;
using Chinesys.UI.Helpers;
using Chinesys.Domain.BizRules;

namespace Chinesys.UI.User
{
    public partial class UsersManagement : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserInRole(Constants.Roles.Administrator);

            Master.PageTitle = "Administración de Usuarios";
            ClearMessages();

            if (!IsPostBack)
            {
                LoadLists();
            }
        }

        protected void grdUsers_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "cmdEditUser")
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    int userID = Convert.ToInt32(((Label)e.Item.FindControl("ID")).Text);
                    LoadUser(userID);
                }
            }

            if (e.CommandName == "cmdDeleteUser")
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    int userID = Convert.ToInt32(((Label)e.Item.FindControl("ID")).Text);
                    DeleteUser(userID);
                }
            }
        }

        private void ShowEditor()
        {
            pnlEditor.Visible = true;
            pnlList.Visible = false;
        }

        private void DeleteUser(int userID)
        {
            try
            {
                UserEntity user = UserDomain.GetByUserID(userID);
                user.Active = false;
                user.CurrentState = RowState.Modified;
                UserDomain.Save(user);

                LoadLists();

                lblInfo.Text = "El usuario ha sido eliminado con exito.";
                lblInfo.Visible = true;
            }
            catch (BizRulesException ex)
            {
                hdnBizRulesException.Value = ex.Message;
            }
        }

        private void LoadUser(int userID)
        {
            UserEntity user = UserDomain.GetByUserID(userID);

            txtId.Text = user.Id.ToString();
            txtName.Text = user.UserName;
            txtPassword.Text = user.Pass;

            UIHelper.SelectComboValue(user.Role.Id.ToString(), ddlRole);

            if (user.Role.RoleName == Constants.Roles.Customer)
                ddlRole.Enabled = false;

            ShowEditor();
        }

        private void ClearMessages()
        {
            validationSummary.Clear();
            lblInfo.Visible = false;
        }

        private void LoadLists()
        {
            grdUsers.DataSource = UserDomain.GetAll();
            grdUsers.DataBind();

            ddlRole.DataSource = RoleDomain.GetAll();
            ddlRole.DataValueField = "Id";
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataBind();
        }
    }
}